import logging

from celery_tasks.main import app
from .yuntongxun.sms import CCP
from django.core.mail import send_mail
from django.conf import settings

from . import constants

logger = logging.getLogger('django')


@app.task(name="send_sms_code")
def send_sms_code(mobile, code):
    """
    发送短信
    :param mobile:
    :param code:
    :param expires:
    :return:
    """
    
    try:
        # 发送短信
        ccp = CCP()
        time = str(constants.SMS_CODE_REDIS_EXPIRES / 60)
        result = 0
        print(code)
        # result = ccp.send_template_sms(mobile, [code, time], constants.SMS_CODE_TEMP_ID)
    except Exception as e:
        logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
    else:
        if result == 0:
            logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile)
        else:
            logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
